package com.sumainfor.pc.market.service;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.HousingInfoEvaMapper;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 房源详表
 * <p>
 *
 * @author: zhoucong
 * @create: 2019-05-30 16:30
 **/
@Service
public class HousingInfoEvaService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(HousingInfoEvaService.class);// 日志

    @Autowired
    HousingInfoEvaMapper housingInfoEvaMapper;

    /**
    * <p>
    * 修改房源详表状态为已签约
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public JsonResult updateHouInfoState(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("更新操作");
            params.put("by", ContextUtils.getUserId());//操作人
            params.put("term", "2");// 1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            params.put("State",2);
            params.put("SigningBody",2);
            params.put("PaId",null);
            Integer IfOperaTypeName = MapUtils.getInteger(params,"IfOperaType");
            if (IfOperaTypeName==1){//整租
              params.remove("HouInfoId");
            }
            Integer cout = housingInfoEvaMapper.updateHouInfoState(params);
            if(cout<=0){
                result.putFailed("修改失败!");
            }else{
                result.putSuccess("修改成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;

    }

    /**
    * <p>
    * 根据房源编号查询房源详表编号
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    public List<Map<String,Object>> getHouInfoId(Map<String,Object> params){
        return housingInfoEvaMapper.getHouInfoId(params);
    }

}
